package br.com.verde.exemploteca.functions

object CollectionGoodness {

  def main(args: Array[String]) {
    val crazyStringNumbers = "1" :: "one" :: "3" :: "4" :: "five" :: Nil
    val stringNumbers = "1" :: "2" :: "3" :: "4" :: "5" :: Nil
    val numbers = 1 :: 2 :: 3 :: 4 :: 5 :: Nil

    println("stringNumbers.map(_ + 3): " + stringNumbers.map(_ + 3))
    println("numbers.map(_ % 2 == 0): " + numbers.map(_ % 2 == 0))

    def evenStringNumberToStringList(s: String) = if (s.toInt % 2 == 0) List(s.toInt) else Nil
    def evenStringNumberToIntList(s: String) = if (s.toInt % 2 == 0) List(s.toDouble) else Nil

    println("stringNumbers.flatMap(evenStringNumberToStringList): " + stringNumbers.flatMap(evenStringNumberToStringList))
    println("stringNumbers.flatMap(evenStringNumberToIntList): " + stringNumbers.flatMap(evenStringNumberToIntList))

    def crazyStringNumbersToIntList(s: String) = {
      try {
        List(s.toInt)
      } catch {
        case _ => Nil
      }
    }

    println("crazyStringNumbers.flatMap(crazyStringNumbersToIntList): " + crazyStringNumbers.flatMap(crazyStringNumbersToIntList))

    println("List(List(1), List(2), List(3), List(4), List(5)).flatten: " + List(List(1), List(2), List(3), List(4), List(5)).flatten)
  }
}